﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace lu596.HT
{
    public partial class frmAddSampleData : Form
    {
        private int currentRowTONGDAI = -1;
        private int currentRowHUONG = -1;
        private int currentRowCAP = -1;
        private int currentRowNHOM_DOI = -1;
        private int currentRowTUHOP = -1;

        public frmAddSampleData()
        {
            InitializeComponent();
        }

        private void frmAddSampleData_Load(object sender, EventArgs e)
        {
            switch (tabControl1.SelectedIndex)
            {
                case 0:

                    break;
                case 1:

                    break;

                default:
                    break;
            }

            // TODO: This line of code loads data into the 'thietBiDataSet.HE_MAY' table. You can move, or remove it, as needed.
            this.hE_MAYTableAdapter.Fill(this.thietBiDataSet.HE_MAY);
            // TODO: This line of code loads data into the 'thietBiDataSet.DUNG_LUONG_KET_NOI' table. You can move, or remove it, as needed.
            this.dUNG_LUONG_KET_NOITableAdapter.Fill(this.thietBiDataSet.DUNG_LUONG_KET_NOI);
            // TODO: This line of code loads data into the 'thietBiDataSet.tblTHIETBI' table. You can move, or remove it, as needed.
            this.tblTHIETBITableAdapter.Fill(this.thietBiDataSet.tblTHIETBI);
            // TODO: This line of code loads data into the 'thietBiDataSet.tblTRAM' table. You can move, or remove it, as needed.
            this.tblTRAMTableAdapter.Fill(this.thietBiDataSet.tblTRAM);
            // TODO: This line of code loads data into the 'thietBiDataSet.tblHUONG' table. You can move, or remove it, as needed.
            this.tblHUONGTableAdapter.Fill(this.thietBiDataSet.tblHUONG);
            // TODO: This line of code loads data into the 'thietBiDataSet.tblTHIETBI_TRAM' table. You can move, or remove it, as needed.
            this.tblTHIETBI_TRAMTableAdapter.Fill(this.thietBiDataSet.tblTHIETBI_TRAM);

            // TODO: This line of code loads data into the 'thietBiDataSet.DAY_DOI' table. You can move, or remove it, as needed.
            this.dAY_DOITableAdapter.Fill(this.thietBiDataSet.DAY_DOI);
            // TODO: This line of code loads data into the 'thietBiDataSet.NHOM_DOI' table. You can move, or remove it, as needed.
            this.nHOM_DOITableAdapter.Fill(this.thietBiDataSet.NHOM_DOI);
            // TODO: This line of code loads data into the 'thietBiDataSet.DUNG_LUONG_CAP' table. You can move, or remove it, as needed.
            this.dUNG_LUONG_CAPTableAdapter.Fill(this.thietBiDataSet.DUNG_LUONG_CAP);
            // TODO: This line of code loads data into the 'thietBiDataSet.CAP' table. You can move, or remove it, as needed.
            this.cAPTableAdapter.Fill(this.thietBiDataSet.CAP);
            // TODO: This line of code loads data into the 'thietBiDataSet.TU_HOP' table. You can move, or remove it, as needed.
            this.tU_HOPTableAdapter.Fill(this.thietBiDataSet.TU_HOP);

            tblTHIETBIBindingSource1.Filter = "MATHIETBI like '03%'";
            tblTHIETBIBindingSource2.Filter = "MATHIETBI like '03%'";
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            this.tblTHIETBITRAMBindingSource.EndEdit();
            this.tblTHIETBI_TRAMTableAdapter.Update(this.thietBiDataSet.tblTHIETBI_TRAM);
            
            this.tblHUONGBindingSource.EndEdit();
            this.tblHUONGTableAdapter.Update(this.thietBiDataSet.tblHUONG);

            this.dUNGLUONGKETNOIBindingSource.EndEdit();
            this.dUNG_LUONG_KET_NOITableAdapter.Update(this.thietBiDataSet.DUNG_LUONG_KET_NOI);
            
            MessageBox.Show("Cập nhật xong.");
        }

        private void btnUpdateDungLuongKetNoi_Click(object sender, EventArgs e)
        {
            this.dUNGLUONGKETNOIBindingSource.EndEdit();
            this.dUNG_LUONG_KET_NOITableAdapter.Update(this.thietBiDataSet.DUNG_LUONG_KET_NOI);
            MessageBox.Show("Cập nhật xong.");
        }

        private void btnUpdateCAP_Click(object sender, EventArgs e)
        {
            this.cAPBindingSource.EndEdit();
            this.cAPTableAdapter.Update(this.thietBiDataSet.CAP);

            this.nHOMDOIBindingSource.EndEdit();
            this.nHOM_DOITableAdapter.Update(this.thietBiDataSet.NHOM_DOI);

            this.dAYDOIBindingSource.EndEdit();
            this.dAY_DOITableAdapter.Update(this.thietBiDataSet.DAY_DOI);

            MessageBox.Show("Cập nhật xong.");
        }

        private void btnThemNhom_Click(object sender, EventArgs e)
        {
            if ((int)dataGridViewCAP.CurrentRow.Cells[5].Value == 0)
            {
                MessageBox.Show("DUNG_LUONG_CAP của table CAP chưa có");
                return;
            }

            if (!string.IsNullOrEmpty(txtSoNhom.Text))
            {
                //Số nhóm trong cáp
                int numNHOM = int.Parse(txtSoNhom.Text);
                
                //======================================================================================
                //Thêm Nhóm đôi
                for (int i = 0; i < numNHOM; i++)
                {
                    //ThietBiDataSet.NHOM_DOIRow rowNHOM = this.thietBiDataSet.NHOM_DOI.NewNHOM_DOIRow();
                    //rowNHOM["CAP_ID"] = dataGridViewCAP.CurrentRow.Cells[0].Value;
                    //rowNHOM["NHOM_SO"] = i + 1;
                    //this.thietBiDataSet.NHOM_DOI.AddNHOM_DOIRow(rowNHOM);
                    DataRow rowNHOM = this.thietBiDataSet.NHOM_DOI.NewRow();
                    rowNHOM["CAP_ID"] = dataGridViewCAP.CurrentRow.Cells[0].Value;
                    rowNHOM["NHOM_SO"] = i + 1;
                    this.thietBiDataSet.NHOM_DOI.Rows.Add(rowNHOM);
                }
                nHOMDOIBindingSource.EndEdit();
                this.nHOM_DOITableAdapter.Update(this.thietBiDataSet.NHOM_DOI);

                //this.nHOM_DOITableAdapter.FillBy(this.thietBiDataSet.NHOM_DOI, ((int)dataGridViewCAP.CurrentRow.Cells[0].Value));
                nHOMDOIBindingSource.Filter = "CAP_ID=" + dataGridViewCAP.CurrentRow.Cells[0].Value.ToString();
                dataGridViewNHOM_DOI.DataSource = nHOMDOIBindingSource;
                currentRowNHOM_DOI = -1;
                ThemDayDoiTheoNhom();
            }
        }

        public void ThemDayDoiTheoNhom()
        {
            //Số nhóm trong cáp
            //int numNHOM = dataGridViewNHOM_DOI.Rows.Count;
            //Số đôi trong mỗi nhóm = Số Đôi Cáp / Số nhóm

            //ThietBiDataSet.DUNG_LUONG_CAPDataTable tableDungLuongCap = new ThietBiDataSet.DUNG_LUONG_CAPDataTable();
            //DataView view = tableDungLuongCap.GetDungLuongCap((int)dataGridViewCAP.CurrentRow.Cells[5].Value);
            //int numDAY_DOI = (int)view[0]["DUNG_LUONG"] / numNHOM;
            int numNHOM = int.Parse(txtSoNhom.Text);
            int numDAY_DOI = int.Parse(txtSoDayDoi.Text);
            //======================================================================================
            //Thêm Dây đôi
            foreach (DataGridViewRow row in dataGridViewNHOM_DOI.Rows)
            {
                for (int j = 0; j < numDAY_DOI; j++)
                {
                    ThietBiDataSet.DAY_DOIRow rowDAY_DOI = this.thietBiDataSet.DAY_DOI.NewDAY_DOIRow();
                    rowDAY_DOI["NHOM_DOI_ID"] = row.Cells[0].Value;
                    rowDAY_DOI["DOI_SO"] = j + 1;
                    rowDAY_DOI["TINH_TRANG_SU_DUNG"] = false;
                    rowDAY_DOI["CHAT_LUONG_SU_DUNG"] = true;
                    this.thietBiDataSet.DAY_DOI.AddDAY_DOIRow(rowDAY_DOI);
                }
            }

            dAYDOIBindingSource.EndEdit();
            this.dAY_DOITableAdapter.Update(this.thietBiDataSet.DAY_DOI);

            //Show in datagridview
            //this.dAY_DOITableAdapter.FillBy(this.thietBiDataSet.DAY_DOI, ((int)dataGridViewNHOM_DOI.CurrentRow.Cells[0].Value));
            dAYDOIBindingSource.Filter = "NHOM_DOI_ID=" + dataGridViewNHOM_DOI.CurrentRow.Cells[0].Value.ToString();
            dataGridViewDAY_DOI.DataSource = dAYDOIBindingSource;

        }

        private void dataGridViewCAP_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dataGridViewCAP.CurrentRow != null)
            {
                //Nếu khác current row
                if (currentRowCAP != dataGridViewCAP.CurrentCell.RowIndex)
                {
                    currentRowCAP = dataGridViewCAP.CurrentCell.RowIndex;
                    //Nếu ko là new row
                    if (currentRowCAP != dataGridViewCAP.Rows.Count - 1)
                    {
                        ThietBiDataSet.DUNG_LUONG_CAPDataTable tableDungLuongCap = new ThietBiDataSet.DUNG_LUONG_CAPDataTable();
                        DataView view = tableDungLuongCap.GetDungLuongCap((int)dataGridViewCAP.CurrentRow.Cells[5].Value);
                        int capacityCAP = (int)view[0]["DUNG_LUONG"];
                        int numDAY_DOI = int.Parse(txtSoDayDoi.Text);
                        int numNHOM = capacityCAP / numDAY_DOI;
                        txtSoNhom.Text = numNHOM.ToString();

                        nHOMDOIBindingSource.Filter = "CAP_ID=" + dataGridViewCAP.CurrentRow.Cells[0].Value.ToString();
                        dataGridViewNHOM_DOI.DataSource = nHOMDOIBindingSource;
                        currentRowNHOM_DOI = -1;
                    }
                    else //Nếu là new row
                    {
                        //defalt value TONGDAI Id
                        dataGridViewCAP.CurrentRow.Cells[1].Value = dataGridViewTONGDAI.CurrentRow.Cells[0].Value;
                        //NHOM_DOI no rows
                        nHOMDOIBindingSource.Filter = "CAP_ID is null";
                        dataGridViewNHOM_DOI.DataSource = nHOMDOIBindingSource;
                        currentRowNHOM_DOI = -1;
                        //DAY_DOI no rows
                        dAYDOIBindingSource.Filter = "NHOM_DOI_ID is null";
                        dataGridViewDAY_DOI.DataSource = dAYDOIBindingSource;
                    }
                }
            }
            else
            {
                currentRowCAP = -1;
                nHOMDOIBindingSource.Filter = "CAP_ID is null";
                dataGridViewNHOM_DOI.DataSource = nHOMDOIBindingSource;
            }

        }

        private void dataGridViewNHOM_DOI_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dataGridViewNHOM_DOI.CurrentCell != null)
            {
                if (currentRowNHOM_DOI != dataGridViewNHOM_DOI.CurrentCell.RowIndex)
                {
                    currentRowNHOM_DOI = dataGridViewNHOM_DOI.CurrentCell.RowIndex;
                    dAYDOIBindingSource.Filter = "NHOM_DOI_ID=" + dataGridViewNHOM_DOI.CurrentRow.Cells[0].Value.ToString();
                    dataGridViewDAY_DOI.DataSource = dAYDOIBindingSource;
                    //this.dAY_DOITableAdapter.FillBy(this.thietBiDataSet.DAY_DOI, ((int)dataGridViewNHOM_DOI.CurrentRow.Cells[0].Value));
                    //Kiểm tra xem Dây đôi trong Nhóm đôi đã có chưa
                    //Nếu chưa có thì thêm vào
                    if (dataGridViewCAP.CurrentRow != null)
                    {
                        if (dataGridViewNHOM_DOI.CurrentRow.Index != dataGridViewNHOM_DOI.Rows.Count - 1
                            && dataGridViewDAY_DOI.Rows.Count == 0)
                            ThemDayDoiTheoNhom();
                    }
                }
            }
            else
            {
                currentRowNHOM_DOI = -1;
                //DAY_DOI no rows
                dAYDOIBindingSource.Filter = "NHOM_DOI_ID is null";
                dataGridViewDAY_DOI.DataSource = dAYDOIBindingSource;
            }
        }

        
        private void dataGridViewHUONG_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dataGridViewHUONG.CurrentCell != null)
            {
                if (currentRowHUONG != dataGridViewHUONG.CurrentCell.RowIndex)
                {
                    currentRowHUONG = dataGridViewHUONG.CurrentCell.RowIndex;
                    dUNGLUONGKETNOIBindingSource.Filter = "HUONG_ID=" + dataGridViewHUONG.CurrentRow.Cells[0].Value.ToString();
                    dataGridViewDUNGLUONGKETNOI.DataSource = dUNGLUONGKETNOIBindingSource;
                    //this.dUNG_LUONG_KET_NOITableAdapter.FillBy(this.thietBiDataSet.DUNG_LUONG_KET_NOI, ((int)dataGridViewHUONG.CurrentRow.Cells[0].Value));

                    //Nếu current row ko phải là dòng thêm mới
                    //Và hiện tại chưa có dữ liệu Dung lượng kết nối
                    if (dataGridViewHUONG.CurrentRow.Index != dataGridViewHUONG.Rows.Count - 1
                        && dataGridViewDUNGLUONGKETNOI.Rows.Count == 1)
                    {
                        this.thietBiDataSet.DUNG_LUONG_KET_NOI.Rows.Add(dataGridViewHUONG.CurrentRow.Cells[0].Value, 1, 0);
                        this.thietBiDataSet.DUNG_LUONG_KET_NOI.Rows.Add(dataGridViewHUONG.CurrentRow.Cells[0].Value, 2, 0);
                        this.thietBiDataSet.DUNG_LUONG_KET_NOI.Rows.Add(dataGridViewHUONG.CurrentRow.Cells[0].Value, 3, 0);
                        this.thietBiDataSet.DUNG_LUONG_KET_NOI.Rows.Add(dataGridViewHUONG.CurrentRow.Cells[0].Value, 4, 0);
                        dUNGLUONGKETNOIBindingSource.EndEdit();
                        dataGridViewDUNGLUONGKETNOI.DataSource = dUNGLUONGKETNOIBindingSource;
                    }
                }
            }
            else
            {
                currentRowHUONG = -1;
            }
        }
        
        private void dataGridViewTONGDAI_CurrentCellChanged(object sender, EventArgs e)
        {
            if (currentRowTONGDAI != dataGridViewTONGDAI.CurrentCell.RowIndex)
            {
                currentRowTONGDAI = dataGridViewTONGDAI.CurrentCell.RowIndex;
                tUHOPBindingSource2.Filter = "THIETBI_TRAM_ID=" + dataGridViewTONGDAI.CurrentRow.Cells[0].Value.ToString();
                dataGridViewTUHOP.DataSource = tUHOPBindingSource2;
                tUHOPDAUBindingSource.Filter = "THIETBI_TRAM_ID=" + dataGridViewTONGDAI.CurrentRow.Cells[0].Value.ToString();
                tUHOPDAUIDDataGridViewTextBoxColumn.DataSource = tUHOPDAUBindingSource;
                tUHOPCUOIBindingSource.Filter = "THIETBI_TRAM_ID=" + dataGridViewTONGDAI.CurrentRow.Cells[0].Value.ToString();
                tUHOPCUOIIDDataGridViewTextBoxColumn.DataSource = tUHOPCUOIBindingSource;
                cAPBindingSource.Filter = "THIETBI_TRAM_ID=" + dataGridViewTONGDAI.CurrentRow.Cells[0].Value.ToString();
                dataGridViewCAP.DataSource = cAPBindingSource;
                //reset row index
                currentRowCAP = -1;
                if (dataGridViewCAP.Rows.Count > 0)
                {
                    dataGridViewCAP.Rows[0].Selected = true;
                    dataGridViewCAP_CurrentCellChanged(null, null);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void btnUpdateTuHop_Click(object sender, EventArgs e)
        {
            tUHOPBindingSource2.EndEdit();
            tU_HOPTableAdapter.Update(thietBiDataSet.TU_HOP);
        }

        
        private void dataGridViewTUHOP_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dataGridViewTUHOP.CurrentRow != null)
            {
                if (currentRowTUHOP != dataGridViewTUHOP.CurrentRow.Index)
                {
                    currentRowTUHOP = dataGridViewTUHOP.CurrentCell.RowIndex;
                    if (currentRowTUHOP == dataGridViewTUHOP.Rows.Count - 1)
                    {
                        dataGridViewTUHOP.CurrentRow.Cells[1].Value = dataGridViewTONGDAI.CurrentRow.Cells[0].Value;
                    }
                }
            }
        }

        
    }
}